<?php

    class Admin_Model_CustomerMapper {
        public function _insert($username, $password, $fistname, $lastname, $email, $phone, $fax, $address, $address2, $city, $user, $birthday) {
            $data = array(
                'username' => $username,
                'password' => $password,
                'firstname' => $fistname,
                'lastname' => $lastname,
                'email' => $email,
                'phone' => $phone,
                'fax' => $fax,
                'address' => $address,
                'address2' => $address2,
                'city' => $city,
                'user' => $user,
                'birthday' => $birthday,
                'regtime' => new Zend_Db_Expr('now()')
            );
            $table = new Application_Model_DbTable_Customer();
            $table->insert($data);

        }

        public function _getAll() {
            // $link = mysql_connect('localhost', 'root', '');
            // mysql_select_db('aitishop', $link);
            // $query = "select * from customer";
            // $result = mysql_query($query);
            // $arr = array();
            // while ($row = mysql_fetch_array($result)) {
            // $customer = new Admin_Model_Customer($row['id'], $row['username'],
            // $row['password'], $row['firstname'], $row['lastname'],
            // $row['email'], $row['phone'], $row['fax'], $row['address'],
            // $row['address2'], $row['city'], $row['user'], $row['birthday'],
            // $row['regtime']);
            // $arr[] = $customer;
            // }
            // return $arr;
            $table = new Application_Model_DbTable_Customer();
            $rowset = $table->fetchAll();
            $arr = array();
            foreach ($rowset as $row) {
                $customer = new Admin_Model_Customer($row->id, $row->username, $row->password, $row->firstname, $row->lastname, $row->email, $row->phone, $row->fax, $row->address, $row->address2, $row->city, $row->user, $row->birthday, $row->regtime);
                $arr[] = $customer;
            }
            return $arr;
        }

        public function _getById($id) {
            $table = new Application_Model_DbTable_Customer();
            $row = $table->fetchRow('id = ' . $id);
            if ($row) {
                $customer = new Admin_Model_Customer($row->id, $row->username, $row->password, $row->firstname, $row->lastname, $row->email, $row->phone, $row->fax, $row->address, $row->address2, $row->city, $row->user, $row->birthday, $row->regtime);
                return $customer;
            }
        }

        public function _delete($id) {
            $table = new Application_Model_DbTable_Customer();
            $row = $table->fetchRow('id=' . $id);
            if ($row) {
                $row->delete();
            }
        }

        public function _update($id, $username, $password, $firstname, $lastname, $email, $phone, $fax, $address, $address2, $city, $user, $birtday) {
            $table = new Application_Model_DbTable_Customer();
            $row = $table->fetchRow('id=' . $id);
            if ($row) {
                $row->username = $username;
                $row->password = $password;
                $row->firstname = $firstname;
                $row->lastname = $lastname;
                $row->email = $email;
                $row->phone = $phone;
                $row->fax = $fax;
                $row->address = $address;
                $row->address2 = $address2;
                $row->city = $city;
                $row->user = $user;
                $row->birthday = $birtday;
                $row->save();
            }
        }

        public function _countAll() {
            $table = new Application_Model_DbTable_Customer();
            $rowset = $table->fetchAll();
            $arr = $rowset->toArray();
            $count = count($arr);
            return $count;
        }

        public function _search($keyword) {
            $table = new Application_Model_DbTable_Customer();
            $keyword = "'%".$keyword."%'";
            //$rowset = $table->fetchAll("username like '". $keyword ."'");
            $rowset = $table->fetchAll('username like ' . $keyword . 'or firstname like ' . $keyword . 'or lastname like ' . $keyword);
            $arr = array();
            foreach ($rowset as $row) {
                $customer = new Admin_Model_Customer($row->id, $row->username, $row->password, $row->firstname, $row->lastname, $row->email, $row->phone, $row->fax, $row->address, $row->address2, $row->city, $row->user, $row->birthday, $row->regtime);
                $arr[] = $customer;
            }
            return $arr;
        }
    }
